using System;
using System.Collections.Generic;
using AdvancedInspector;
using Rilisoft.MiniJson;
using UnityEngine;

[AdvancedInspector(true)]
public class ChestBonusController : MonoBehaviour
{
	public delegate void OnChestBonusEnabledDelegate();

	public ChestBonusData _bonusesData = new ChestBonusData();

	public InAppProduct chooseProdCoins;

	private float _lastCheckEventTime;

	private bool _lastBonusActive;

	private DateTime _timeStartBonus;

	private DateTime _timeEndBonus;

	private bool _isGetBonusInfoRunning;

	private float _eventGetBonusInfoStartTime;

	public static ChestBonusController instance { get; private set; }

	public bool IsEventBonusActive
	{
		get
		{
			return IsBonusActive;
		}
	}

	public bool IsBonusActive
	{
		get
		{
			if (_bonusesData == null || _bonusesData.timeStart == 0 || _bonusesData.duration == 0)
			{
				return false;
			}
			DateTime utcNow = DateTime.UtcNow;
			return utcNow >= _timeStartBonus && utcNow <= _timeEndBonus;
		}
	}

	public static event OnChestBonusEnabledDelegate OnChestBonusChange;

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		_timeStartBonus = default(DateTime);
		_timeEndBonus = default(DateTime);
		ParseData(JSONManager.Instance.GetJSON(URLs.ChestBonus));
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Update()
	{
		if (Time.realtimeSinceStartup - _lastCheckEventTime >= 1f && IsBonusActive)
		{
			if (_lastBonusActive != IsBonusActive && ChestBonusController.OnChestBonusChange != null)
			{
				ChestBonusController.OnChestBonusChange();
				_lastBonusActive = IsBonusActive;
			}
			_lastCheckEventTime = Time.realtimeSinceStartup;
		}
	}

	private void ParseData(string text)
	{
		Dictionary<string, object> dictionary = Json.Deserialize(text) as Dictionary<string, object>;
		if (dictionary == null)
		{
			BCWDebug.LogError("DownloadDataAboutBonuses bonusesData = null");
			_isGetBonusInfoRunning = false;
			return;
		}
		if (dictionary.ContainsKey("start"))
		{
			_bonusesData.timeStart = Convert.ToInt32((long)dictionary["start"]);
		}
		if (dictionary.ContainsKey("duration"))
		{
			_bonusesData.duration = Convert.ToInt32((long)dictionary["duration"]);
		}
		if (_bonusesData.timeStart == 0 || _bonusesData.duration == 0)
		{
			_isGetBonusInfoRunning = false;
			return;
		}
		if (!dictionary.ContainsKey("chest"))
		{
			_isGetBonusInfoRunning = false;
			return;
		}
		List<object> list = dictionary["chest"] as List<object>;
		if (list != null)
		{
			_bonusesData.bonuses = new List<ChestBonusItem>();
			foreach (object item in list)
			{
				Dictionary<string, object> dictionary2 = (Dictionary<string, object>)item;
				ChestBonusItem chestBonusItem = new ChestBonusItem();
				chestBonusItem.linkKey = dictionary2["linkKey"].ToString();
				chestBonusItem.isVisible = Convert.ToInt32(dictionary2["isVisible"]) == 1;
				List<object> list2 = (List<object>)dictionary2["tirs"];
				if (list2 != null)
				{
					chestBonusItem.tirs = new List<ChestTir>();
					foreach (object item2 in list2)
					{
						Dictionary<string, object> dictionary3 = (Dictionary<string, object>)item2;
						ChestTir chestTir = new ChestTir();
						chestTir.items = new List<ChestBonusItemData>();
						chestTir.startLevel = Convert.ToInt32(dictionary3["startLevel"]);
						List<object> list3 = (List<object>)dictionary3["items"];
						if (list3 == null)
						{
							continue;
						}
						foreach (object item3 in list3)
						{
							Dictionary<string, object> dictionary4 = (Dictionary<string, object>)item3;
							ChestBonusItemData chestBonusItemData = new ChestBonusItemData();
							chestBonusItemData.id = dictionary4["id"].ToString();
							chestBonusItemData.count = Convert.ToInt32(dictionary4["count"]);
							chestBonusItemData.timeLife = Convert.ToInt32(dictionary4["timeLife"]);
							chestTir.items.Add(chestBonusItemData);
						}
						chestBonusItem.tirs.Add(chestTir);
					}
				}
				_bonusesData.bonuses.Add(chestBonusItem);
			}
		}
		_timeStartBonus = GetCurrentTimeByUnixTime(_bonusesData.timeStart);
		int unixTime = _bonusesData.timeStart + _bonusesData.duration;
		_timeEndBonus = GetCurrentTimeByUnixTime(unixTime);
		if (ChestBonusController.OnChestBonusChange != null)
		{
			ChestBonusController.OnChestBonusChange();
		}
		_isGetBonusInfoRunning = false;
	}

	public ChestBonusItem GetChestById(string idCh)
	{
		for (int i = 0; i < _bonusesData.bonuses.Count; i++)
		{
			if (_bonusesData.bonuses[i].linkKey == idCh)
			{
				return _bonusesData.bonuses[i];
			}
		}
		return null;
	}

	public void ShowBonusWindowForItem(ChestBonusItem chestItem, Texture icon, InAppProduct curProdCoins = null)
	{
		if (chestItem != null)
		{
			chooseProdCoins = curProdCoins;
			if (BonusesInChestWindow.instance != null)
			{
				BonusesInChestWindow.instance.ShowWindowForChest(chestItem, curProdCoins, icon);
			}
		}
	}

	private void PrintDataChest()
	{
		string text = "__PrintDataChest :\n";
		string text2 = text;
		text = text2 + "start: " + _bonusesData.timeStart + "\n";
		text2 = text;
		text = text2 + "duration: " + _bonusesData.duration + "\n";
		text2 = text;
		text = text2 + "Activate: " + IsBonusActive + "\n";
		BCWDebug.Log(text);
		foreach (ChestBonusItem bonuse in _bonusesData.bonuses)
		{
			PrintChest(bonuse);
		}
	}

	private static void PrintChest(ChestBonusItem itemBonuse)
	{
		string text = "PrintChest " + itemBonuse.linkKey + "\n";
		foreach (ChestTir tir in itemBonuse.tirs)
		{
			string text2 = text;
			text = text2 + "lev: " + tir.startLevel + "\n";
			foreach (ChestBonusItemData item in tir.items)
			{
				text = text + "item: " + item.id + "\n";
			}
		}
	}

	public static bool TryTakeChestBonus(ChestBonusItem itemBonuse)
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("TryTakeChestBonus: " + itemBonuse.linkKey);
		}
		ChestBonusController chestBonusController = instance;
		if (chestBonusController == null)
		{
			return false;
		}
		if (!chestBonusController.IsBonusActive)
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("Chest Bonus no Active");
			}
			return false;
		}
		if (itemBonuse == null)
		{
			return false;
		}
		ChestTir chestTirForCurrentLevel = itemBonuse.GetChestTirForCurrentLevel();
		if (chestTirForCurrentLevel == null || chestTirForCurrentLevel.items == null || chestTirForCurrentLevel.items.Count == 0)
		{
			return false;
		}
		for (int i = 0; i < chestTirForCurrentLevel.items.Count; i++)
		{
			string id = chestTirForCurrentLevel.items[i].id;
			if (!string.IsNullOrEmpty(id))
			{
				if (BCWDebug.isDebugBuild)
				{
					BCWDebug.Log("buy item chest: " + id);
				}
				int addCount;
				try
				{
					addCount = Convert.ToInt32(chestTirForCurrentLevel.items[i].GetItemCountOrTime());
				}
				catch
				{
					addCount = 0;
				}
				MarketController.Instance.BuyProduct(id, true, addCount);
			}
		}
		return true;
	}

	public static DateTime GetCurrentTimeByUnixTime(int unixTime)
	{
		return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTime);
	}
}
